import pandas as pd
import numpy as np

#merge的合并相对concat有更多可实现

left = pd.DataFrame({'key':['k0','k1','k2','k3'],
                     'a':['a0','a1','a2','a3'],
                     'b':['b0','b1','b2','b3']})
right = pd.DataFrame({'key':['k0','k1','k2','k3'],
                     'c':['c0','c1','c2','c3'],
                     'd':['d0','d1','d2','d3']})

print(left)
print(right)
res = pd.merge(left,right,on='key')     #基于key这个column进行合并
print(res,'----------1')

left = pd.DataFrame({'key1':['k0','k0','k1','k2'],
                     'key2':['k0','k1','k0','k1'],
                     'a':['a0','a1','a2','a3'],
                     'b':['b0','b1','b2','b3']})
right = pd.DataFrame({'key1':['k0','k1','k2','k3'],
                      'key2':['k0','k0','k0','k0'],
                      'c':['c0','c1','c2','c3'],
                      'd':['d0','d1','d2','d3']})
res = pd.merge(left,right,on=['key1','key2'])    #默认合并的方法是inner
print(res,'-------------2')


#how = [left rignt inner outer]
res = pd.merge(left,right,on=['key1','key2'],how='outer')
print(res)

#indicator
df1 = pd.DataFrame({'col1':[0,1],
                    'cll_left':['a','b']})
df2 = pd.DataFrame({'col1':[1,1,2],
                    'cll_right':[2,2,2]})
print(df1,'indicator-------------df1')
print(df2,'indicator-------------df2')
res=pd.merge(df1,df2,on='col1',how='outer',indicator=True)    #显示merge
print(res,'indicator----------res')

#index                      名字重复，含义不同，可以用哦
left = pd.DataFrame({'key1':['k0','k0','k1','k2'],
                     'key2':['k0','k1','k0','k1'],
                     'a':['a0','a1','a2','a3']},
                     index=['t0','t1','t2','t3'])
right = pd.DataFrame({'key1':['k0','k1','k2','k3'],
                      'key2':['k0','k0','k0','k0'],
                      'c':['c0','c1','c2','c3']},
                      index=['t0','t1','t2','t3'])
print(left,'index---------left')
print(right,'index------------right')
res = pd.merge(left,right,left_index=True,right_index=True,suffixes=['_left','_right'],how='outer')
print(res,'index----------------res')

